package com.qbb.client.exception;

import java.util.Date;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.qbb.client.constant.FlagAttr;
import com.qbb.client.util.DateUtil;

/**
 * 获取异常类相关信息
 * 
 * @author chenhuaiao
 * @version V1.0
 * @createTime 2014年5月14日 上午9:34:49
 */
public class HandlerException {
	private static Log lg = LogFactory.getLog(HandlerException.class);

	private HandlerException() {
	}

	/**
	 * 记录平台的异常日志信息
	 * 
	 * @param e
	 */
	public static void error(Exception e) {
		StringBuffer buffer = new StringBuffer(480);
		try {
			buffer.append(e);
			StackTraceElement stack[] = e.getStackTrace();
			int len = 0;
			if (stack != null) {
				len = stack.length;
			}
			buffer.append("\n");
			for (int i = 0; i < len; i++) {
				StackTraceElement ste = stack[i];
				String classname = ste.getClassName();
				if (classname == null) {
					continue;
				}
				// if (classname.indexOf("com.qbb") == -1) {
				// continue;
				// }
				buffer.append("classname:" + ste.getClassName() + ";");
				buffer.append("methodname: " + ste.getMethodName() + ";");
				buffer.append("filename:" + ste.getFileName() + ";");
				buffer.append("linenumber:" + ste.getLineNumber() + ";");
				buffer.append("\n");
			}
		} catch (Exception e2) {
			buffer.append(e2.getMessage());
		}

		String resultinfo = buffer.toString();

		Date date = new Date();
		String nowTime = DateUtil.date_YYYY_MM_DD_MM_HH_SS(date);

		lg.error("handlerException have exception nowtime:[" + nowTime
				+ "],message:[" + resultinfo + "]");
	}

	/**
	 * 调用请求，返回的成功
	 * 
	 * @param msg
	 * @return
	 */
	public static Map<String, Object> successMsg(Object obj) {
		Map<String, Object> map = new ConcurrentHashMap<String, Object>();

		map.put(FlagAttr.SUCCESS, FlagAttr.TRUE);
		map.put(FlagAttr.DATA, obj);

		return map;
	}

	/**
	 * 调用请求，返回的错误提示信息
	 * 
	 * @param msg
	 * @return
	 */
	public static Map<String, Object> returnMsg(String msg) {
		Map<String, Object> map = new ConcurrentHashMap<String, Object>();

		map.put(FlagAttr.SUCCESS, FlagAttr.FALSE);
		map.put(FlagAttr.MSG, msg);

		return map;
	}
}
